<template>
    <empty :msg.sync="message" :show-empty="taskList.length==0"></empty>
    <div class="page form-report show-tabbar yf-kqsetup" v-if="taskList.length">
        <div class="mod-form com-border-top" v-for="(index,task) in taskList">
            <div class="topbar_3 com-border-bottom">
                <div class="info">
                    <!-- <h2>{{task.name}}</h2> -->
                    <div>
                        <span class="task_title">{{task.name}}</span>
                        <div class="state" v-if="taskBtnShow">{{task.status==0?'已禁用':'进行中'}}<input class="weui-switch" type="checkbox" v-model="task.status" :true-value="1" :false-value="0" @change="updateTask(index,task.id,task.status)"></div>
                    </div>
                    <template v-if="task.work_type == 2">                        
                        <p class="time"><span class="time_type">人员排班</span></p>
                            <div class="detail_info">
                                <ul>
                                    <li class="kqset_item" v-if="task.task_schedules.length >= 1">
                                        <span class="item_title">班次A</span>
                                        <p class="detail_time" v-if="task.task_schedules[0].cross == 1">{{task.task_schedules[0].start_time}} - 次日 {{task.task_schedules[0].end_time}}</p>
                                        <p class="detail_time" v-else>{{task.task_schedules[0].start_time}} - {{task.task_schedules[0].end_time}}</p>
                                    </li>
                                    <li class="kqset_item" v-if="task.task_schedules.length >= 2">
                                        <span class="time_title">班次B</span>
                                        <p class="detail_time" v-if="task.task_schedules[1].cross == 1">{{task.task_schedules[1].start_time}} - 次日 {{task.task_schedules[1].end_time}}</p>
                                        <p class="detail_time" v-else>{{task.task_schedules[1].start_time}} - {{task.task_schedules[1].end_time}}</p>
                                    </li>
                                    <li class="kqset_item" v-if="task.task_schedules.length >= 3">
                                        <span class="time_title">班次C</span>
                                        <p class="detail_time" v-if="task.task_schedules[2].cross == 1">{{task.task_schedules[2].start_time}} - 次日 {{task.task_schedules[2].end_time}}</p>
                                        <p class="detail_time" v-else>{{task.task_schedules[2].start_time}} - {{task.task_schedules[2].end_time}}</p>
                                    </li>
                                </ul>
                            </div>
                            <div class="suspension_points" v-if="task.task_schedules.length > 3">…</div>
                    </template>
                    <template v-if="task.work_type == 1">                        
                        <p class="time">
                            <span class="time_type no_schedule" :class="{'iosStyle':iosStyle,'andriodStyle':!iosStyle}">弹性时间</span>
                             <span class="detail_date" ><span v-for="(index,weekItem) in task.work_week"><span v-if="index != 0">、</span>{{weekItem}}</span></span>
                        </p>
                        <div class="detail_info">
                            <p class="kqset_item"><span class="item_title">工作{{task.work_time_length}}小时</span></p>
                        </div>
                    </template>
                    <template v-if="task.work_type == 0">                        
                        <p class="time">
                            <span class="time_type no_schedule" :class="{'iosStyle':iosStyle,'andriodStyle':!iosStyle}">固定时间</span> 
                            <span class="detail_date" ><span v-for="(index,weekItem) in task.work_week"><span v-if="index != 0">、</span>{{weekItem}}</span></span>
                        </p>
                        <div class="detail_info">
                            <ul v-for="(index,workTime) in task.work_time_range">
                                <li class="kqset_item">
                                    <span class="item_title" v-if="index == 0">时段一</span>
                                    <span class="item_title" v-if="index == 1">时段二</span>
                                    <span class="item_title" v-if="index == 2">时段三</span>
                                    <p class="detail_time" v-if="workTime.cross == 1">{{workTime.start.time}} - 次日{{workTime.end.time}}</p>
                                    <p class="detail_time" v-else>{{workTime.start.time}} - {{workTime.end.time}}</p>
                                </li>
                            </ul>
                        </div>
                    </template>
                </div>
                <!-- <div class="state" v-if="taskBtnShow">{{task.status==0?'已禁用':'进行中'}}<input class="weui-switch" type="checkbox" v-model="task.status" :true-value="1" :false-value="0" @change="updateTask(index,task.id,task.status)"></div> -->
            </div>            
            <ul class="opt-box">
                <li @click="taskEdit(task.id)" v-if="taskBtnShow"><a class="touch" href="#">规则设置</a></li>                
                <li @click="classesEdit(task.id,task.status)" v-if="task.work_type == 2 && scheduleBtnShow"><a class="touch" href="#" >排班管理</a></li>
                <!-- <li @click="showShare(task.id)" v-if="task.work_type != 2"><a class="touch" href="#"><span class="icon icon_3 bg_sprite"></span>分享</a></li> -->
            </ul>            
        </div>
    </div>
     <div class="page__bd" v-show="showMore">
        <div class="weui-loadmore">
            <i class="weui-loading"></i>
            <span class="weui-loadmore__tips">正在加载</span>
        </div>
      </div>
</template>

<script>
    /**
     *  引入工具类库 或 Native API
     *  example : import widget from 'gapi/widget'
     */
    
    /**
     *  引入组件 以及 组件接口(Action)
     *  example : import Somecomponents from 'components/Somecomponents/Somecomponents'
     */
    import widget from 'gapi/widget'
    import cache from 'gapi/cache.js'
    import commonPage from 'clientApi/commonPage.js'
    import socialApi from 'clientApi/socialApi.js'
    import * as util from 'js/util.js' 
    import Empty from 'components/Empty/Empty'
    import clientUi from 'gapi/clientUi'
    import KCApiApp from 'clientApi/KCApiApp.js'
    commonPage.canPullWebView(false)



    let pageDate = {}
    let pageIndex = 0
    let canLoadMore = 1

    

     //https://vuxjs.gitbooks.io/vux/content/message/actionsheet.html
    /**
     *  组件实例对象
     */
    export default {
        components: {
            Empty
        },
        data: function(){
            return{
                showBut:false,
                taskList:[],
                message:'暂无考勤规则',
                showMore:false,
                taskBtn: 1,
                taskBtnShow: true,
                scheduleBtnShow: false,
                iosStyle:true,
            }
        },
        vuex: {
            getters: {

            },
            actions: {

            }
        },
        methods: {
            /**
             * 初始化
             * @method init
             * @public
             * @return {Null} void
             */
            init(){
                cache.getCache('KqCacheMessge',(data)=>{
                    pageDate.project_id = data.project_id
                    pageDate.team_id = data.team_id
                    this.getKqDate()
                    this.getPermissions()

                })
                //ios和安卓样式不兼容
                if(util.OS() =='ios'){
                    this.iosStyle = true
                }else{
                    this.iosStyle = false
                }

                
            },
            getKqDate (){
               if(!this.showMore){
                  commonPage.loadPageStatus('loading')
                }
                util.ajax({
                    url: '/attendance/attendance_task_list',
                    data: {
                        'team_id': pageDate.team_id,
                        'project_id': pageDate.project_id,
                        // 'page_size':10,
                        // 'min_id':pageIndex
                    },
                    success: (res) => {
                        if(!this.showMore){
                          commonPage.loadPageStatus('loadSuccess')
                        }else{
                          this.showMore = false
                        }
                        commonPage.loadPageStatus('loadSuccess')
                        // alert(JSON.stringify(res.data))
                        if(res&&res.errno==0){
                            this.taskBtn = res.data.page_data.show_edit_btn                            
                            if(this.taskBtn == 0){
                                this.taskBtnShow = false                                
                            }else{
                                this.taskBtnShow = true
                            }
                            /**
                             * 设置 添加考勤任务按钮
                             * @type {String}
                             */
                            commonPage.updateTitleBar({
                                backAction: '',
                                leftDisplay: false,
                                leftText: '',
                                leftAction: '',
                                rightDisplay: this.taskBtnShow,
                                rightText: '添加',
                                rightAction: 'addKqTask'
                            })

                            if(res.data){
                                // if(pageIndex==0){
                                //     this.taskList = res.data.list
                                // }else{
                                //     res.data.list.forEach((item)=>{
                                //         this.taskList.push(item)
                                //     })
                                // }
                                this.taskList = res.data.list
                                this.taskList.forEach((obj) => {
                                    if(obj.work_week.length != 0){
                                        obj.work_week.forEach((item) =>  {
                                            if(item == 1){    
                                                let i = obj.work_week.indexOf(item)
                                                obj.work_week.splice(i,1,'周一')
                                            }else if(item == 2){
                                                let i = obj.work_week.indexOf(item)
                                                obj.work_week.splice(i,1,'周二')
                                            }else if(item == 3){
                                                let i = obj.work_week.indexOf(item)
                                                obj.work_week.splice(i,1,'周三')
                                            }else if(item == 4){
                                                let i = obj.work_week.indexOf(item)
                                                obj.work_week.splice(i,1,'周四')
                                            }else if(item == 5){
                                                let i = obj.work_week.indexOf(item)
                                                obj.work_week.splice(i,1,'周五')
                                            }else if(item == 6){
                                                let i = obj.work_week.indexOf(item)
                                                obj.work_week.splice(i,1,'周六')
                                            }else if(item == 7){
                                                let i = obj.work_week.indexOf(item)
                                                obj.work_week.splice(i,1,'周日')
                                            }
                                        })
                                    }
                                })

                                console.log(123456+JSON.stringify(this.taskList))
                                // pageIndex = res.data.page_data.min_id
                                canLoadMore = res.data.page_data.load_more
                            }
                        }else if(res.errmsg){
                            commonPage.loadPageStatus('loadSuccess')
                            widget.toast(res.errmsg)
                        }else{
                            commonPage.loadPageStatus('loadSuccess')
                            widget.toast('数据获取失败')
                        }
                        
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    },
                    noNetwork: () => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            updateTask(index,task_id,status){
                // let loadStatus = status==0?1:0
                let loadStatus = status
                commonPage.loadPageStatus('loading')
                util.ajax({
                    url: '/attendance/attendance_task_status/update',
                    type:'POST',
                    data: {
                        team_id:pageDate.team_id,
                        project_id: pageDate.project_id,
                        task_id:task_id,
                        status:loadStatus
                    },
                    success: (res) => {
                        // res = JSON.parse(res) 
                        console.log(JSON.stringify(res)) 
                        commonPage.loadPageStatus('loadSuccess')
                        if(res&&res.errno==0){
                            if(status == 0){
                                //埋点
                                let statisticsLog_data = {
                                    event_id: '考勤规则管理-关闭考勤规则',
                                    // event_label: data.event_label,(预留，现在不用传此参数)
                                    event_param: {
                                        ca_from: 'closerule',
                                        project_id: pageDate.project_id,
                                    }
                                }
                                // alert(JSON.stringify(statisticsLog_data))
                                KCApiApp.statisticsLog(statisticsLog_data)
                            }else if(status == 1){
                                //埋点
                                let statisticsLog_data = {
                                    event_id: '考勤规则管理-开启考勤规则',
                                    // event_label: data.event_label,(预留，现在不用传此参数)
                                    event_param: {
                                        ca_from: 'addrule',
                                        project_id: pageDate.project_id,
                                    }
                                }
                                // alert(JSON.stringify(statisticsLog_data))
                                KCApiApp.statisticsLog(statisticsLog_data)
                            }

                            this.taskList[index].status = loadStatus
                        }else if(res.errno == 15011){
                            widget.toast(res.errmsg)
                            this.taskList[index].status = 0
                        }else if(res.errmsg){
                             widget.toast(res.errmsg)
                        }else{
                            widget.toast('数据获取失败')
                        }
                    },
                    error: (xhr, status) => {
                        commonPage.loadPageStatus('loadSuccess')
                        widget.toast('网络连接失败，请检查网络')
                    },
                    noNetwork: () => {
                      commonPage.loadPageStatus('loadSuccess')
                      widget.toast('网络连接失败，请检查网络')
                    }
                })
            },
            taskEdit(id){
                //埋点
                let statisticsLog_data = {
                    event_id: '考勤规则管理-修改规则',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'modifyrule',
                        project_id: pageDate.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)

                window.location.href = "dmsaas://KaoqinEdit?urdData="+encodeURIComponent(`task_id=${id}&editType=99`)
            },
            taskEdit_add(id){
                window.location.href = "dmsaas://KaoqinTaskSet"
            },
            // 分享
            showShare(task_id){
                util.ajax({
                    url: '/attendance/share_attendance_task',
                    data: {
                        team_id:pageDate.team_id,
                        project_id: pageDate.project_id,
                        task_id:task_id
                    },
                    success: (res) => {
                        // console.log(res)
                        if(res&&res.errno==0){
                            res.data.shareType=7
                            socialApi.shareKQTask(res.data)
                        }else if(res.errmsg){
                             widget.toast(res.errmsg)
                        }else{
                          widget.toast('分享数据获取失败')
                        }
                    },
                    error: (xhr, status) => {
                        widget.toast('网络连接失败，请检查网络')
                    },
                    noNetwork: () => {
                      widget.toast('网络连接失败，请检查网络')
                    }
                })
                
            },
            // 编辑排班
            classesEdit(task_id,task_status){
                if(task_status == 0){
                    widget.toast('该考勤规则已禁用')
                }else{
                    //埋点
                    let statisticsLog_data = {
                        event_id: '考勤规则管理-排班',
                        // event_label: data.event_label,(预留，现在不用传此参数)
                        event_param: {
                            ca_from: 'schedule',
                            project_id: pageDate.project_id,
                        }
                    }
                    // alert(JSON.stringify(statisticsLog_data))
                    KCApiApp.statisticsLog(statisticsLog_data)
                    alert(1234)
                    window.location.href="dmsaas://KaoqinScheduleEdit?"+`task_id=${task_id}&type=`+'set'
                }
            },
             //权限获取
            getPermissions (){
                util.ajax({
                    url:'/permission/application',
                    type:'GET',
                    data:{
                        team_id: pageDate.team_id,
                        project_id: pageDate.project_id,
                        application_id:0
                    },
                    timeout:10000,
                    success:(obj) => {
                        // console.log(JSON.stringify(obj))
                        if(obj&&obj.errno == 0){
                            obj.data.forEach ( (i) => {
                                //判断是否有权对成员进行操作
                                if(i.id_name == 'attendance_set_user_schedule'){  
                                    this.scheduleBtnShow = true   
                                }
                            })
                        }else{
                            widget.toast(obj.errmsg)
                        }
                    },
                    error: (xhr, status) => {
                        widget.toast('服务器异常')
                    },
                    noNetwork: () => {
                        widget.toast('网络连接失败，请检查网络')
                    }
                })
            }
        },
        created(){
            this.init()
            commonPage.getWebViewID((data)=>{
                console.log(data)
                cache.setMemoryCache('editKqViewID',{'editKqViewID':data})
            })
            // /**
            //  * 设置 添加考勤任务按钮
            //  * @type {String}
            //  */
            // commonPage.updateTitleBar({
            //     backAction: '',
            //     leftDisplay: false,
            //     leftText: '',
            //     leftAction: '',
            //     rightDisplay: this.taskBtnShow,
            //     rightText: '添加',
            //     rightAction: 'addKqTask'
            // })

            //添加考勤
            window.addKqTask = (data)=>{
                //埋点
                let statisticsLog_data = {
                    event_id: '考勤规则管理-添加',
                    // event_label: data.event_label,(预留，现在不用传此参数)
                    event_param: {
                        ca_from: 'openrule',
                        project_id: pageDate.project_id,
                    }
                }
                // alert(JSON.stringify(statisticsLog_data))
                KCApiApp.statisticsLog(statisticsLog_data)
                this.taskEdit_add()
            }

            window.loadData = (data)=>{
                              
                if(data.key == 'yy_kqtaskset'){
                  pageIndex = 0
                  canLoadMore = 1
                  this.getKqDate()
                  widget.toast('提交成功')
                }else if(data.key == 'yy_kqtaskdelete'){
                  pageIndex = 0
                  canLoadMore = 1
                  this.getKqDate()
                  widget.toast('删除成功')
                }
            }
            commonPage.registerHitPageBottomListener(() => {
                if(canLoadMore==0){
                    widget.toast('没有更多数据')
                    return
                }
                // this.showMore = true
                // this.getKqDate()
            }, 0)
            
        }
        
    }

    
</script>

<!-- CSS 样式 -->
<style src="../../node_modules/weui/dist/style/weui.css"></style>
<style src="../../static/css/common.css"></style>
<style src="../../static/css/form-report.css"></style>
<style>
    @media (max-width: 325px){
        .detail_date {
            color: rgb(153,153,153);
            display: inline-block;
            width: 60%;
            overflow: hidden;
            text-overflow: ellipsis;
            white-space: nowrap;
            padding-left: 21px;
        }
    }
</style>

